using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class StockItemOrder
	{
		public int GetMaxId()
		{
			return Conn.GetMaxID("ItemOrderID", "HLJXC_StockItemOrder");
		}
		public bool Exists(int ItemOrderID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from HLJXC_StockItemOrder");
			stringBuilder.Append(" where ItemOrderID=@ItemOrderID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ItemOrderID", SqlDbType.Int, 4)
			};
			array[0].Value = ItemOrderID;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public int Add(HKB.Data.Model.StockItemOrder model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HLJXC_StockItemOrder(");
			stringBuilder.Append("InfoOrderID,InfoNumber,ProductID,StockCount,Unit,Price,OldPrice)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@InfoOrderID,@InfoNumber,@ProductID,@StockCount,@Unit,@Price,@OldPrice)");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@InfoOrderID", SqlDbType.Int, 4),
				new SqlParameter("@InfoNumber", SqlDbType.NVarChar, 50),
				new SqlParameter("@ProductID", SqlDbType.Int, 4),
				new SqlParameter("@StockCount", SqlDbType.Int, 4),
				new SqlParameter("@Unit", SqlDbType.NVarChar, 50),
				new SqlParameter("@Price", SqlDbType.Decimal, 9),
				new SqlParameter("@OldPrice", SqlDbType.Decimal, 9)
			};
			array[0].Value = model.InfoOrderID;
			array[1].Value = model.InfoNumber;
			array[2].Value = model.ProductID;
			array[3].Value = model.StockCount;
			array[4].Value = model.Unit;
			array[5].Value = model.Price;
			array[6].Value = model.OldPrice;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public int AddInfoTransaction(HKB.Data.Model.StockItemOrder model, Conn conn)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HLJXC_StockItemOrder(");
			stringBuilder.Append("InfoOrderID,InfoNumber,ProductID,StockCount,Unit,Price,OldPrice)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@InfoOrderID,@InfoNumber,@ProductID,@StockCount,@Unit,@Price,@OldPrice)");
			stringBuilder.Append(" select SCOPE_IDENTITY()");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@InfoOrderID", SqlDbType.Int, 4),
				new SqlParameter("@InfoNumber", SqlDbType.NVarChar, 50),
				new SqlParameter("@ProductID", SqlDbType.Int, 4),
				new SqlParameter("@StockCount", SqlDbType.Int, 4),
				new SqlParameter("@Unit", SqlDbType.NVarChar, 50),
				new SqlParameter("@Price", SqlDbType.Decimal, 9),
				new SqlParameter("@OldPrice", SqlDbType.Decimal, 9)
			};
			array[0].Value = model.InfoOrderID;
			array[1].Value = model.InfoNumber;
			array[2].Value = model.ProductID;
			array[3].Value = model.StockCount;
			array[4].Value = model.Unit;
			array[5].Value = model.Price;
			array[6].Value = model.OldPrice;
			object obj = conn.ExecuteTransactionScalar(stringBuilder.ToString(), array);
			int result;
			if (obj == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(obj);
			}
			return result;
		}
		public bool Update(HKB.Data.Model.StockItemOrder model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update HLJXC_StockItemOrder set ");
			stringBuilder.Append("InfoOrderID=@InfoOrderID,");
			stringBuilder.Append("InfoNumber=@InfoNumber,");
			stringBuilder.Append("ProductID=@ProductID,");
			stringBuilder.Append("StockCount=@StockCount,");
			stringBuilder.Append("Unit=@Unit,");
			if (model.OldPrice.HasValue)
			{
				stringBuilder.Append("OldPrice=" + model.OldPrice + ",");
			}
			stringBuilder.Append("Price=@Price");
			stringBuilder.Append(" where ItemOrderID=@ItemOrderID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@InfoOrderID", SqlDbType.Int, 4),
				new SqlParameter("@InfoNumber", SqlDbType.NVarChar, 50),
				new SqlParameter("@ProductID", SqlDbType.Int, 4),
				new SqlParameter("@StockCount", SqlDbType.Int, 4),
				new SqlParameter("@Unit", SqlDbType.NVarChar, 50),
				new SqlParameter("@Price", SqlDbType.Decimal, 9),
				new SqlParameter("@ItemOrderID", SqlDbType.Int, 4)
			};
			array[0].Value = model.InfoOrderID;
			array[1].Value = model.InfoNumber;
			array[2].Value = model.ProductID;
			array[3].Value = model.StockCount;
			array[4].Value = model.Unit;
			array[5].Value = model.Price;
			array[6].Value = model.ItemOrderID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(int ItemOrderID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HLJXC_StockItemOrder ");
			stringBuilder.Append(" where ItemOrderID=@ItemOrderID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ItemOrderID", SqlDbType.Int, 4)
			};
			array[0].Value = ItemOrderID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string ItemOrderIDlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HLJXC_StockItemOrder ");
			stringBuilder.Append(" where ItemOrderID in (" + ItemOrderIDlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public HKB.Data.Model.StockItemOrder GetModel(int ItemOrderID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 * from HLJXC_StockItemOrder ");
			stringBuilder.Append(" where ItemOrderID=@ItemOrderID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ItemOrderID", SqlDbType.Int, 4)
			};
			array[0].Value = ItemOrderID;
			HKB.Data.Model.StockItemOrder stockItemOrder = new HKB.Data.Model.StockItemOrder();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			HKB.Data.Model.StockItemOrder result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public HKB.Data.Model.StockItemOrder DataRowToModel(DataRow row)
		{
			HKB.Data.Model.StockItemOrder stockItemOrder = new HKB.Data.Model.StockItemOrder();
			if (row != null)
			{
				if (row["ItemOrderID"] != null && row["ItemOrderID"].ToString() != "")
				{
					stockItemOrder.ItemOrderID = int.Parse(row["ItemOrderID"].ToString());
				}
				if (row["InfoOrderID"] != null && row["InfoOrderID"].ToString() != "")
				{
					stockItemOrder.InfoOrderID = new int?(int.Parse(row["InfoOrderID"].ToString()));
				}
				if (row["InfoNumber"] != null)
				{
					stockItemOrder.InfoNumber = row["InfoNumber"].ToString();
				}
				if (row["ProductID"] != null && row["ProductID"].ToString() != "")
				{
					stockItemOrder.ProductID = new int?(int.Parse(row["ProductID"].ToString()));
				}
				if (row["StockCount"] != null && row["StockCount"].ToString() != "")
				{
					stockItemOrder.StockCount = new int?(int.Parse(row["StockCount"].ToString()));
				}
				if (row["Unit"] != null)
				{
					stockItemOrder.Unit = row["Unit"].ToString();
				}
				if (row["Price"] != null && row["Price"].ToString() != "")
				{
					stockItemOrder.Price = new decimal?(decimal.Parse(row["Price"].ToString()));
				}
				if (row["OldPrice"] != System.DBNull.Value)
				{
					stockItemOrder.OldPrice = new decimal?(System.Convert.ToDecimal(row["OldPrice"].ToString()));
				}
			}
			return stockItemOrder;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select * FROM HLJXC_StockItemOrder ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" * FROM HLJXC_StockItemOrder ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public int GetRecordCount(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) FROM HLJXC_StockItemOrder ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT * FROM ( ");
			stringBuilder.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				stringBuilder.Append("order by T." + orderby);
			}
			else
			{
				stringBuilder.Append("order by T.ItemOrderID desc");
			}
			stringBuilder.Append(")AS Row, T.*  from HLJXC_StockItemOrder T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				stringBuilder.Append(" WHERE " + strWhere);
			}
			stringBuilder.Append(" ) TT");
			stringBuilder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
	}
}
